package com.MusicWeb.servlet;

import com.MusicWeb.entity.Music;
import com.MusicWeb.service.UsermusicService;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/select")
//todo: 音乐模糊查询
//http://localhost:8080/music/select
public class MusicselectServlet extends HttpServlet {
    UsermusicService usermusicService = new UsermusicService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("music_name");
        List<Music> list = usermusicService.Musiclist(name);
        if (list.size()!=0){
            // 设置返回的数据为json对象
            resp.setContentType("application/json;charset=utf-8");
            // 把实体转换为json，返回给前端
            String str = new Gson().toJson(list);
            resp.getWriter().print(str);
            //返回提交的音乐信息
        } else {
            req.getSession().setAttribute("msg", "查询失败");
        }

    }
}
